﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using ActiveStoryTouch.DataModel;
using System.IO;

namespace ASTouchService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ASTDataModelService" in code, svc and config file together.
    public class ASTDataModelService : IASTDataModelService
    {
        private static Dictionary<Guid, SessionManager> _activeSessionManagers;

        private void Init()
        {
            if (_activeSessionManagers == null)
                _activeSessionManagers = new Dictionary<Guid, SessionManager>();
        }

       #region IASTDataModelService Members

        public Guid BeginSession()
        {
            Init();
            Guid newSessionId = Guid.NewGuid();
            SessionManager newSessionManager = new SessionManager { SessionId = newSessionId };
            _activeSessionManagers.Add(newSessionId, newSessionManager);
            return newSessionId;
        }

        public String GetSessionInstructions()
        {
            return SessionManager.CurrentSessionSettings.TaskInstructions;
        }

        public Page GetInitialPage(Guid sessionId)
        {
            if (sessionId == null)
                return null;
            return _activeSessionManagers[sessionId].CurrentProject.PageDictionary[SessionManager.CurrentSessionSettings.InitialPage];
        }

        public Page GetPageWithId(Guid sessionId, long identifier)
        {
            if (sessionId == null)
                return null;
            return _activeSessionManagers[sessionId].CurrentProject.PageDictionary[identifier];
        }

        public void EndSession(Guid sessionId)
        {
            // TODO: do any necessary end of session work here (data collection, etc)
            _activeSessionManagers.Remove(sessionId);
        }

        public GestureManager GetGestureManager()
        {
            return SessionManager.CommonGestureManager;
        }

        #endregion

        //public SerializableStrokeCollection TestStrokeSending()
        //{
        //    FileStream fs = new FileStream(@"C:\Users\Alessandro Del Piero\Documents\Visual Studio 2010\Projects\SilverlightWoZTouch\ASTouchService\TestStrokes.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
        //    DataContractSerializer seriazlier = new DataContractSerializer(typeof(SerializableStrokeCollection));
        //    Object o = seriazlier.ReadObject(fs);
        //    SerializableStrokeCollection sc = o as SerializableStrokeCollection;
        //    fs.Close();

        //    return sc;
        //}

        
    }
}
